library(lattice)

rm(list=ls())

# Impulse Response Functions
x <- rep(0,11)
x[2] <- 1
ystatic <- rep(0,11)
yfinite <- rep(0,11)
yldv    <- rep(0,11)
yadl    <- rep(0,11)

ystatic <- 2*x

for(t in 2:11){
  yadl[t]    <- 0.2*x[t] + 0.8*x[t-1] + 0.5*yadl[t-1]
}

rm(t)

for(t in 2:11){
  yldv[t]    <- x[t] + 0.5*yldv[t-1]
  }

rm(t)
for(t in 2:11){
  yfinite[t] <- 1.5*x[t] + 0.5*x[t-1]
}

ystatic <- ystatic[-1]
yfinite <- yfinite[-1]
yldv    <- yldv[-1]
yadl    <- yadl[-1]

year <-  1:10
year4 <- rep(year,4)
Y <- c(ystatic,yfinite,yldv,yadl)
which <-  rep(c("AR1","FDL","LDV", "ADL"),c(10,10,10,10))

gcolor <-  c("red","darkgreen", "blue", "black")
superpose.line <- trellis.par.get("superpose.line")
superpose.line$col <- gcolor
superpose.line$lwd <- 1.5
superpose.line$lty <- c(1,2,4,3,5,6,7)

add.text <- trellis.par.get("add.text")
add.text$cex <- 0.5

trellis.device("pdf",file="irf.pdf",theme="col.whitebg",  onefile=F,
            width=6, height=3)
trellis.par.set("superpose.line",superpose.line)
trellis.par.set("add.text",add.text)

par(mar=c(1,1,0,0),xaxs="i",yaxs="i",las=1)
print(xyplot(Y ~ year4,
             xlab="Year",
             ylab="Response",
             aspect=1/2,
             groups=which,
             type="l",
             panel = panel.superpose,
             cex=0.5,
             auto.key= list(x=0.95,y=0.95,corner=c(1,1), lines=T,
                points=F,cex=.5)
             )
      )
dev.off()
rm(list=ls())

# Unit Response Functions
x <- rep(1,11)
x[1] <- 0
ystatic <- rep(0,11)
yfinite <- rep(0,11)
yldv    <- rep(0,11)
yadl    <- rep(0,11)

ystatic <- 2*x

for(t in 2:11){
  yadl[t]    <- 0.2*x[t] + 0.8*x[t-1] + 0.5*yadl[t-1]
}

rm(t)

for(t in 2:11){
  yldv[t]    <- x[t] + 0.5*yldv[t-1]
  }

rm(t)
for(t in 2:11){
  yfinite[t] <- 1.5*x[t] + 0.5*x[t-1]
}

ystatic <- ystatic[-1]
yfinite <- yfinite[-1]
yldv    <- yldv[-1]
yadl    <- yadl[-1]

year <-  1:10
year4 <- rep(year,4)
Y <- c(ystatic,yfinite,yldv,yadl)
which <-  rep(c("AR1","FDL","LDV", "ADL"),c(10,10,10,10))

gcolor <-  c("red","darkgreen", "blue", "black")
superpose.line <- trellis.par.get("superpose.line")
superpose.line$col <- gcolor
superpose.line$lwd <- 1.5
superpose.line$lty <- c(1,2,4,3,5,6,7)

add.text <- trellis.par.get("add.text")
add.text$cex <- 0.5

trellis.device("pdf",file="urf.pdf",theme="col.whitebg",  onefile=F,
            width=6, height=3)
trellis.par.set("superpose.line",superpose.line)
trellis.par.set("add.text",add.text)

par(mar=c(1,1,0,0),xaxs="i",yaxs="i",las=1)
print(xyplot(Y ~ year4,
             xlab="Year",
             ylab="Response",
             aspect=1/2,
             groups=which,
             type="l",
             panel = panel.superpose,
             cex=0.5,
             auto.key= list(x=0.95,y=0.30,corner=c(1,1), lines=T,
               points=F,cex=.5)
             )
      )
dev.off()
